<?php 
namespace JCAT\Tools\BuildPHAR ;
use JCAT\PDRL ;
use JCAT\PDRL\DHtml ;
use JCAT\Common\FS ;


class Dialog extends DHtml\DHtmlDialog
{
	/**
	 * Description
	 *
	 * @access	public
	 * @return	void
	 */
	public function OnInitWindow($aEventOb)
	{
		parent::OnInitWindow($aEventOb) ;
		
		$aWnd = $this->GetWindow() ;
		$aWnd->SetSize(400,300) ;
		
		$this->SetEventHandle('btnBrowseSource',DHTML_ONCLICK,array($this,'OnBrowseSource')) ;
		$this->SetEventHandle('btnBrowsePHARPath',DHTML_ONCLICK,array($this,'OnBrowsePHAR')) ;
		$this->SetEventHandle('btnBuild',DHTML_ONCLICK,array($this,'OnBuild')) ;
		$this->SetEventHandle('btnClose',DHTML_ONCLICK,array($this,'OnClose')) ;
		
		$this->Navigate("file:///".__DIR__."/index.html") ;
	}
	
	/**
	 * 事件：网页已经读取完成
	 *
	 * @access	public
	 * @param	$aEventOb	object	事件数据
	 * @return	int
	 */
	public function OnNavigateComplete($aEventOb)
	{
		$this->SetElementValue('iptSourceFolder',realpath(__DIR__.'/../../Framework')) ;		
		$this->SetElementValue('iptPHARPath',__DIR__.'/jcat.phar') ;		
	}

	/**
	 * Description
	 *
	 * @access	public
	 * @return	void
	 */
	public function OnBrowseSource()
	{
		$sFolder = pdr_browse_folder() ;
		if($sFolder)
		{
			$this->SetElementValue('iptSourceFolder',$sFolder) ;
		}
	}
	/**
	 * Description
	 *
	 * @access	public
	 * @return	void
	 */
	public function OnBrowsePHAR()
	{
		$sFile = pdr_browse_file() ;
		if($sFile)
		{
			$this->SetElementValue('iptPHARPath',$sFile) ;
		}
	}
	/**
	 * Description
	 *
	 * @access	public
	 * @return	void
	 */
	public function OnBuild()
	{
		if(!$sSourceFolder = $this->GetElementValue('iptSourceFolder'))
		{
			pdr_window_msg(PDRL\i('请输入源目录')) ;
			return ;
		}
		if(!$sPHARPath = $this->GetElementValue('iptPHARPath') )
		{
			pdr_window_msg(PDRL\i('请输入PHAR路径')) ;
			return ;
		}
		
		$sFilename = basename($sPHARPath) ;
		@unlink($sPHARPath) ;

		$aPhar = new \Phar('/path/to/my.phar', CURRENT_AS_FILEINFO|KEY_AS_FILENAME,$sFilename);
		$aPhar->startBuffering();
		
		$aIterator = new FS\FSOTreeIterator($sSourceFolder) ;
		for ( $aIterator->First(); !$aIterator->IsDone(); $aIterator->Next() )
		{
			echo $sFile = $aIterator->Current();
		}
		
		
		$aPhar->stopBuffering();
		
		/*$sPHPPath = pdr_get_php_path() ;
		$sDir = __DIR__ ;
		$sCmd = "\"{$sPHPPath}\" -f \"{$sDir}/Package.php\" \"{$sSourceFolder}\" \"{$sPHARPath}\"" ;
		//$sCmd = "{$sPHPPath} -m" ;
		if(!$hProcess=popen($sCmd,'r'))
		{
			pdr_window_msg(PDRL\i('遇到错误')) ;
			return ;
		}
		while ( !feof($hProcess) )
		{
			echo $sOutput=fgets($hProcess,1024) ;
		}
		
		pclose($hProcess) ;*/
	}
	/**
	 * Description
	 *
	 * @access	public
	 * @return	void
	 */
	public function OnClose()
	{
		$this->EndModalCancel() ;
	}
}

?>